package com.future.util;

public class CommonUtil {

    /**
     * 随机产生[0,1);
     *
     * @return
     */
    public static double getRandomInt() {
        return Math.random();
    }

    /**
     * @param maxLength
     * @param maxValue
     * @return
     */
    public static int[] generateRandomArr(int maxLength, int maxValue) {
        int realLength = (int) (Math.random() * maxLength);
        int[] arr = new int[realLength];
        for (int i = 0; i < realLength; i++) {
            arr[i] = (int) (Math.random() * maxLength);
        }
        return arr;
    }

    /**
     * 打印链表
     *
     * @param head
     */
    public static void printListNode(ListNode head) {
        while (head != null) {
            System.out.println(head.val);
            head = head.next;
        }
    }


    /**
     * 先序遍历：中左右
     */
    public static void preOrder(TreeNode head) {
        if (head == null) {
            return;
        }
        System.out.print(head.val);
        preOrder(head.left);
        preOrder(head.right);
    }

    /**
     * 中序遍历：左中右
     */
    public static void middleOrder(TreeNode head) {
        if (head == null) {
            return;
        }
        middleOrder(head.left);
        System.out.print(head.val);
        middleOrder(head.right);
    }

    /**
     * 后序遍历：左右后
     */
    public static void postOrder(TreeNode head) {
        if (head == null) {
            return;
        }
        postOrder(head.left);
        postOrder(head.right);
        System.out.print(head.val);
    }
}
